jQuery(function($) {
				//initiate dataTables plugin
				var myTable = 
				$('#dynamic-table').DataTable( 
						{
					        "processing": true, //处理数据的时候显示 处理中。。。。。
					        "serverSide": true, //运用在服务器端，必须指定一个aJaxsource数据源
					        "order":[[0,"desc"]],		        
					    	 "columnDefs":[		    	         
					    	            {"orderable":false,"targets":[0,1]}, 
					    	            {"orderable":false,"targets":[0,2]}, 
					                    {"orderable":false,"targets":[0,3]}, 
					                    {"orderable":false,"targets":[0,5]},
					                    {"orderable":false,"targets":[0,6]},
					                    {"orderable":false,"targets":[0,7]}, 
					                    {"orderable":false,"targets":[0,8]},
					                    {"orderable":false,"targets":[0,10]},	
					    	              ],
					        
					        "ajax": {
					            "url": "index.php?r=merchandis/datainfo",
					            "type": "POST"
					        },
					        
					        //语言国际化
					        language:{				        	
					        	"url":"aceAsset/i18n/zh_cn.json",
					        },
					            
					        
					        "columns": [
					            { "data": "chose" },
					            { "data": "MerchandiseID" },
					            { "data": "ProStatusID" },
					            { "data": "MerchandiseName" },
					            { "data": "Price" },	
					            { "data": "MerchandiseAB" },
					           				            
					            { "data": "SaleStatus" },
					            { "data": "Spec" },            //商品规格
					    		{ "data": "PicPath" },          //图片展示
					    		{ "data": "ClickCount" },       //点击量
                                { "data": "options" }
					        ]
					    }
						
						
				);
							
				$.fn.dataTable.Buttons.swfPath = "aceAsset/js/dataTables/extensions/buttons/swf/flashExport.swf"; //in Ace demo ../assets will be replaced by correct assets path
				$.fn.dataTable.Buttons.defaults.dom.container.className = 'dt-buttons btn-overlap btn-group btn-overlap';
				
				new $.fn.dataTable.Buttons( myTable, {
					buttons: [
					  {
						"extend": "colvis",
						"text": "<i class='fa fa-search bigger-110 blue'></i> <span class='hidden'>Show/hide columns</span>",
						"className": "btn btn-white btn-primary btn-bold",
						columns: ':not(:first):not(:last)'
					  },
					  {
						"extend": "copy",
						"text": "<i class='fa fa-copy bigger-110 pink'></i> <span class='hidden'>Copy to clipboard</span>",
						"className": "btn btn-white btn-primary btn-bold"
					  },
					  {
						"extend": "csv",
						"text": "<i class='fa fa-database bigger-110 orange'></i> <span class='hidden'>Export to CSV</span>",
						"className": "btn btn-white btn-primary btn-bold"
					  },
					  {
						"extend": "excel",
						"text": "<i class='fa fa-file-excel-o bigger-110 green'></i> <span class='hidden'>Export to Excel</span>",
						"className": "btn btn-white btn-primary btn-bold"
					  },
					  {
						"extend": "pdf",
						"text": "<i class='fa fa-file-pdf-o bigger-110 red'></i> <span class='hidden'>Export to PDF</span>",
						"className": "btn btn-white btn-primary btn-bold"
					  },
					  {
						"extend": "print",
						"text": "<i class='fa fa-print bigger-110 grey'></i> <span class='hidden'>Print</span>",
						"className": "btn btn-white btn-primary btn-bold",
						autoPrint: false,
						message: 'This print was produced using the Print button for DataTables'
					  }		  
					]
				} );
				myTable.buttons().container().appendTo( $('.tableTools-container') );
				
				//style the message box
				var defaultCopyAction = myTable.button(1).action();
				myTable.button(1).action(function (e, dt, button, config) {
					defaultCopyAction(e, dt, button, config);
					$('.dt-button-info').addClass('gritter-item-wrapper gritter-info gritter-center white');
				});
				
				
				var defaultColvisAction = myTable.button(0).action();
				myTable.button(0).action(function (e, dt, button, config) {
					
					defaultColvisAction(e, dt, button, config);
					
					
					if($('.dt-button-collection > .dropdown-menu').length == 0) {
						$('.dt-button-collection')
						.wrapInner('<ul class="dropdown-menu dropdown-light dropdown-caret dropdown-caret" />')
						.find('a').attr('href', '#').wrap("<li />")
					}
					$('.dt-button-collection').appendTo('.tableTools-container .dt-buttons')
				});
			
				////
			
				setTimeout(function() {
					$($('.tableTools-container')).find('a.dt-button').each(function() {
						var div = $(this).find(' > div').first();
						if(div.length == 1) div.tooltip({container: 'body', title: div.parent().text()});
						else $(this).tooltip({container: 'body', title: $(this).text()});
					});
				}, 500);
				
				
				
				
				
				myTable.on( 'select', function ( e, dt, type, index ) {
					if ( type === 'row' ) {
						$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', true);
					}
				} );
				myTable.on( 'deselect', function ( e, dt, type, index ) {
					if ( type === 'row' ) {
						$( myTable.row( index ).node() ).find('input:checkbox').prop('checked', false);
					}
				} );
			
			
			
			
				/////////////////////////////////
				//table checkboxes
				$('th input[type=checkbox], td input[type=checkbox]').prop('checked', false);
				
				//select/deselect all rows according to table header checkbox
				$('#dynamic-table > thead > tr > th input[type=checkbox], #dynamic-table_wrapper input[type=checkbox]').eq(0).on('click', function(){
					var th_checked = this.checked;//checkbox inside "TH" table header
					
					$('#dynamic-table').find('tbody > tr').each(function(){
						var row = this;
						if(th_checked) myTable.row(row).select();
						else  myTable.row(row).deselect();
					});
				});
				
				//select/deselect a row when the checkbox is checked/unchecked
				$('#dynamic-table').on('click', 'td input[type=checkbox]' , function(){
					var row = $(this).closest('tr').get(0);
					if(!this.checked) myTable.row(row).deselect();
					else myTable.row(row).select();
				});
			
			
			
				$(document).on('click', '#dynamic-table .dropdown-toggle', function(e) {
					e.stopImmediatePropagation();
					e.stopPropagation();
					e.preventDefault();
				});
				
				
				
				//And for the first simple table, which doesn't have TableTools or dataTables
				//select/deselect all rows according to table header checkbox
				var active_class = 'active';
				$('#simple-table > thead > tr > th input[type=checkbox]').eq(0).on('click', function(){
					var th_checked = this.checked;//checkbox inside "TH" table header
					
					$(this).closest('table').find('tbody > tr').each(function(){
						var row = this;
						if(th_checked) $(row).addClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', true);
						else $(row).removeClass(active_class).find('input[type=checkbox]').eq(0).prop('checked', false);
					});
				});
				
				//select/deselect a row when the checkbox is checked/unchecked
				$('#simple-table').on('click', 'td input[type=checkbox]' , function(){
					var $row = $(this).closest('tr');
					if(this.checked) $row.addClass(active_class);
					else $row.removeClass(active_class);
				});
			
				
			
				/********************************/
				//add tooltip for small view action buttons in dropdown menu
				$('[data-rel="tooltip"]').tooltip({placement: tooltip_placement});
				
				//tooltip placement on right or left
				function tooltip_placement(context, source) {
					var $source = $(source);
					var $parent = $source.closest('table')
					var off1 = $parent.offset();
					var w1 = $parent.width();
			
					var off2 = $source.offset();
					//var w2 = $source.width();
			
					if( parseInt(off2.left) < parseInt(off1.left) + parseInt(w1 / 2) ) return 'right';
					return 'left';
				}
				
			
			})
			
	
           //商品详情信息查看			
         function merchandisview(id){
			
					$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
						_title: function(title) {
							var $title = this.options.title || '&nbsp;'
							if( ("title_html" in this.options) && this.options.title_html == true )
								title.html($title);
							else title.text($title);
						}
					}));    		      
			       //ajax异步请求数据
					var id = id;						
					$.ajax({
						type:"post",
						url:"index.php?r=merchandis/merchandisinfoview",
						data:{	
								"action":"merchandisview_action",
								"id":id,				
							},
						dataType:"html",
						success:function(msg){
							$("#dialog-message").html(msg);
						}
					});
					var dialog = $( "#dialog-message" ).removeClass('hide').dialog({
						modal: true,
						width:400,
						title: "<div class='widget-header widget-header-small'><h4 class='smaller'><i class='ace-icon fa fa-check'></i> 商品信息查看</h4></div>",
						title_html: true,
						//按钮
					});			
	           	}



      //商品详情信息修改		
     function merchandisupdate(id){
			
			$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
				_title: function(title) {
					var $title = this.options.title || '&nbsp;'
					if( ("title_html" in this.options) && this.options.title_html == true )
						title.html($title);
					else title.text($title);
				}
			}));    
		      var id = id;		    
			   $.ajax({
			    	type:"post",
			    	url:"index.php?r=merchandis/merchandisupdate",
			    	data:{
			    		"action":"merchandis_update",
			    		"id":id,			    		
			    	},
			    	dataType:"html", 
			    	success:function(msg){			    		
			    		$("#dialog-message").html(msg);
			    	} 			    	 
			     });
					var dialog = $( "#dialog-message" ).removeClass('hide').dialog({
						modal: true,
						width:650,
						title: "<div class='widget-header widget-header-small'><h4 class='smaller'><i class='ace-icon fa fa-check'></i> 商品信息修改</h4></div>",
						title_html: true,
						//按钮
						buttons: [ 
									{
										text: "取消",
										"class" : "btn btn-minier",
										click: function() {
											$( this ).dialog( "close" ); 
										} 
									},
									{
										text: "修改",
										"class" : "btn btn-primary btn-minier",
										click: function() {
											$( "#merchandisup" ).submit(); 
										} 
									}
									
								]
					});			
	           	}
			

				//商品详情信息删除				
				function merchandisdele(id){
				    var id = id;
					bootbox.setLocale("zh_CN");
					bootbox.confirm("是否确定删除?",function(result){
						if(result) {
							$.ajax({
								type:"post",
								url:"index.php?r=merchandis/merchandisinfodele",
								data:{id:id},
								success:function(msg){
									if(msg !== "修改成功")
										alert(msg);
									else
										myTable.draw( false );			
								}
							})
						}
					});										
			     }


				//商品类别选择
				 function merchandisinfosort(id){
						
						$.widget("ui.dialog", $.extend({}, $.ui.dialog.prototype, {
							_title: function(title) {
								var $title = this.options.title || '&nbsp;'
								if( ("title_html" in this.options) && this.options.title_html == true )
									title.html($title);
								else title.text($title);
							}
						}));    
					      var id = id;		    
						   $.ajax({
						    	type:"post",
						    	url:"index.php?r=merchandis/merchandisupdate",
						    	data:{
						    		"action":"merchandis_update",
						    		"id":id,			    		
						    	},
						    	dataType:"html", 
						    	success:function(msg){			    		
						    		$("#dialog-message").html(msg);
						    	} 			    	 
						     });
								var dialog = $( "#dialog-sort" ).removeClass('hide').dialog({
									modal: true,
									width:450,
									title: "<div class='widget-header widget-header-small'><h4 class='smaller'><i class='ace-icon fa fa-check'></i> 商品类别选择</h4></div>",
									title_html: true,
									//按钮
								});			
				           	}


				           //商品助记码触发事件
							$("#merchandisform-merchandisename").keyup(function(){	
 
								 var name = $(this).val();							    
								 $url=$("#w0").attr("action");  //index.php?r=merchandis/index
								
								 $.ajax({
									  type:"post",
									  url:$url,
									  data:{
										  "action":"ab",
										  names:name
									  },
									  //dataType:"html",
									  success:function(msg){
											$("#merchandisform-merchandiseab").empty().val(msg);
									  }		
								  });	 	 	 
							})

			                 
							//商品类别选择触发事件
							$("#merchandisform-merchandisecid").blur(function(){
								
								var value = $(this).val();
								$.ajax({
									type:"post",
								     url:"index.php?r=merchandis/merchautodata",								   
								     dataType:"html",
								     data:{
								    	 "action":"kkk", 
								    	  value:value
								     },
									success:function(msg){												
									  $("#merchandisform-merchandisecid").empty().val(msg);
									}										
								});							
							})
				

			
			
